Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slice/Array intrinsics: __slice and __elem_at #6282

Merged
merged 31 commits into from
Aug 7, 2024
Merged

Conversation

xunilrj
Copy link
Contributor

@xunilrj xunilrj commented Jul 19, 2024

Description

This PR is part of #5110 and introduces two new intrinsic: __slice and __elem_at.

__slice allows the creation of slices by slicing arrays or other slices. Whilst __elem_at returns a reference to an item inside the slice or the array.

Out of bounds checks

These intrinsic will not generate any runtime checks, these must be done manually, when and where appropriate; but they do a complete static analysis of all indices, to avoid runtime buffer overflows, when possible.

That means that at runtime, it is possible to do a buffer overflow when reading/writing, which is an "undefined behaviour" as to what will happen.

Empty Array

This PR also solves a problem with empty arrays. Before empty arrays such as let a = [] were being type-checked as [Never; 0], which means that any code after them was being marked as dead.

Now we correctly type check them as [Unknown; 0] and return a more friendly error.

          4 |
          5 |     // Empty array
          6 |     let a = [];
            |             ^^ Type must be known at this point
          7 | }
            |
          ____

Check of constants inside fns

This PR also solves a problem with not checking const expressions inside fns. We, for example, do not allow slices in constants, but we were only checking globals. Now we check constants also inside functions, methods etc...

Small improvements for our e2e

We can now dbg inside our e2e harness and get results like the ones below. One needs to include the lib test/src/e2e_vm_tests/utils and cal something.dbg() or something.dbgln(). There is no magic, and structs/enums will need to manually implement the Dbg trait.

This is only to facilitate the debugging of our e2e tests.

image

Checklist

  • I have linked to any relevant issues.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have updated the documentation where relevant (API docs, the reference, and the Sway book).
  • I have added tests that prove my fix is effective or that my feature works.
  • I have added (or requested a maintainer to add) the necessary Breaking* or New Feature labels where relevant.
  • I have done my best to ensure that my PR adheres to the Fuel Labs Code Review Standards.
  • I have requested a review from the relevant team or maintainers.

Copy link

Benchmark for d3d6dc2

Click to view benchmark
Test Base PR %
code_action 5.2±0.03ms 5.1±0.13ms -1.92%
code_lens 286.1±4.47ns 315.2±7.85ns +10.17%
compile 2.6±0.03s 2.6±0.06s 0.00%
completion 4.6±0.01ms 4.7±0.37ms +2.17%
did_change_with_caching 2.6±0.04s 2.6±0.02s 0.00%
document_symbol 843.7±22.02µs 886.4±10.66µs +5.06%
format 68.2±0.67ms 69.1±1.10ms +1.32%
goto_definition 340.2±7.60µs 341.5±10.92µs +0.38%
highlight 9.0±0.14ms 8.7±0.21ms -3.33%
hover 493.1±3.62µs 492.8±6.81µs -0.06%
idents_at_position 121.6±0.94µs 117.6±0.53µs -3.29%
inlay_hints 641.8±7.73µs 629.9±11.49µs -1.85%
on_enter 473.1±15.04ns 499.6±18.78ns +5.60%
parent_decl_at_position 3.7±0.03ms 3.6±0.09ms -2.70%
prepare_rename 338.8±6.28µs 341.3±6.96µs +0.74%
rename 9.2±0.04ms 9.1±0.20ms -1.09%
semantic_tokens 1196.5±11.34µs 1280.4±36.18µs +7.01%
token_at_position 331.2±13.62µs 335.0±6.98µs +1.15%
tokens_at_position 3.7±0.05ms 3.6±0.13ms -2.70%
tokens_for_file 399.9±8.09µs 407.1±3.73µs +1.80%
traverse 36.7±0.87ms 36.9±1.31ms +0.54%

Copy link

Benchmark for 79c9a60

Click to view benchmark
Test Base PR %
code_action 5.0±0.04ms 5.2±0.04ms +4.00%
code_lens 337.8±9.28ns 283.1±8.91ns -16.19%
compile 2.7±0.06s 2.6±0.04s -3.70%
completion 4.6±0.13ms 4.7±0.08ms +2.17%
did_change_with_caching 2.6±0.03s 2.7±0.04s +3.85%
document_symbol 868.5±30.49µs 927.5±23.10µs +6.79%
format 68.6±0.61ms 74.0±1.04ms +7.87%
goto_definition 341.3±6.94µs 441.4±11.66µs +29.33%
highlight 8.6±0.12ms 8.8±0.02ms +2.33%
hover 493.1±5.30µs 612.4±9.61µs +24.19%
idents_at_position 120.3±0.31µs 117.1±0.86µs -2.66%
inlay_hints 624.5±23.86µs 730.8±7.81µs +17.02%
on_enter 458.6±19.09ns 474.2±14.31ns +3.40%
parent_decl_at_position 3.6±0.03ms 3.6±0.03ms 0.00%
prepare_rename 339.9±7.79µs 443.4±13.24µs +30.45%
rename 8.9±0.12ms 9.1±0.03ms +2.25%
semantic_tokens 1210.4±19.42µs 1287.1±15.47µs +6.34%
token_at_position 337.8±2.72µs 426.8±12.20µs +26.35%
tokens_at_position 3.6±0.04ms 3.6±0.03ms 0.00%
tokens_for_file 408.0±3.39µs 401.8±1.98µs -1.52%
traverse 38.0±0.42ms 37.5±0.46ms -1.32%

Copy link

Benchmark for 421d20c

Click to view benchmark
Test Base PR %
code_action 5.0±0.01ms 5.2±0.08ms +4.00%
code_lens 339.3±11.08ns 286.4±8.77ns -15.59%
compile 2.6±0.06s 2.6±0.03s 0.00%
completion 4.5±0.15ms 4.7±0.02ms +4.44%
did_change_with_caching 2.6±0.03s 2.6±0.04s 0.00%
document_symbol 839.2±13.78µs 904.0±7.84µs +7.72%
format 69.4±0.84ms 69.5±1.09ms +0.14%
goto_definition 341.5±3.13µs 341.8±6.52µs +0.09%
highlight 8.7±0.16ms 9.0±0.13ms +3.45%
hover 498.8±5.93µs 494.7±4.58µs -0.82%
idents_at_position 119.1±0.43µs 122.9±0.60µs +3.19%
inlay_hints 633.8±7.53µs 643.2±22.88µs +1.48%
on_enter 460.7±6.38ns 476.6±8.45ns +3.45%
parent_decl_at_position 3.6±0.03ms 3.7±0.02ms +2.78%
prepare_rename 341.8±6.47µs 340.7±5.16µs -0.32%
rename 8.9±0.13ms 9.3±0.12ms +4.49%
semantic_tokens 1199.8±8.35µs 1259.2±10.64µs +4.95%
token_at_position 337.8±1.29µs 329.1±2.58µs -2.58%
tokens_at_position 3.5±0.02ms 3.7±0.02ms +5.71%
tokens_for_file 405.6±2.21µs 399.9±3.16µs -1.41%
traverse 38.3±1.09ms 37.6±0.45ms -1.83%

Copy link

Benchmark for f6b311d

Click to view benchmark
Test Base PR %
code_action 5.1±0.12ms 5.1±0.09ms 0.00%
code_lens 337.7±6.82ns 288.3±11.38ns -14.63%
compile 2.7±0.07s 2.7±0.07s 0.00%
completion 4.6±0.07ms 4.5±0.10ms -2.17%
did_change_with_caching 2.7±0.03s 2.7±0.04s 0.00%
document_symbol 888.2±21.67µs 894.5±25.95µs +0.71%
format 69.7±0.75ms 69.8±1.10ms +0.14%
goto_definition 340.6±8.19µs 347.1±7.05µs +1.91%
highlight 8.6±0.18ms 8.8±0.20ms +2.33%
hover 496.0±7.98µs 501.2±7.52µs +1.05%
idents_at_position 121.7±0.61µs 119.9±0.88µs -1.48%
inlay_hints 628.2±13.10µs 643.5±26.53µs +2.44%
on_enter 460.7±7.67ns 466.6±12.27ns +1.28%
parent_decl_at_position 3.6±0.04ms 3.6±0.07ms 0.00%
prepare_rename 349.2±6.82µs 346.7±6.13µs -0.72%
rename 9.0±0.17ms 9.1±0.27ms +1.11%
semantic_tokens 1235.9±9.52µs 1253.0±12.56µs +1.38%
token_at_position 339.2±2.55µs 345.2±6.30µs +1.77%
tokens_at_position 3.6±0.04ms 3.6±0.05ms 0.00%
tokens_for_file 403.2±7.00µs 401.5±3.08µs -0.42%
traverse 39.3±1.22ms 39.3±0.72ms 0.00%

Copy link

Benchmark for 43ea4e9

Click to view benchmark
Test Base PR %
code_action 5.0±0.04ms 5.4±0.04ms +8.00%
code_lens 348.9±39.13ns 335.7±15.32ns -3.78%
compile 2.7±0.04s 2.6±0.05s -3.70%
completion 4.6±0.09ms 4.6±0.13ms 0.00%
did_change_with_caching 2.6±0.05s 2.6±0.04s 0.00%
document_symbol 895.1±37.28µs 886.0±20.48µs -1.02%
format 70.4±2.40ms 69.7±1.95ms -0.99%
goto_definition 345.1±11.10µs 339.9±5.86µs -1.51%
highlight 8.7±0.16ms 8.6±0.16ms -1.15%
hover 505.3±7.84µs 496.1±4.79µs -1.82%
idents_at_position 119.7±0.47µs 119.0±0.87µs -0.58%
inlay_hints 630.1±21.46µs 641.8±19.09µs +1.86%
on_enter 456.0±11.63ns 478.9±20.01ns +5.02%
parent_decl_at_position 3.6±0.06ms 3.6±0.08ms 0.00%
prepare_rename 379.0±7.08µs 337.8±4.57µs -10.87%
rename 8.9±0.04ms 9.0±0.21ms +1.12%
semantic_tokens 1187.3±7.66µs 1241.1±39.20µs +4.53%
token_at_position 351.9±2.25µs 334.9±3.26µs -4.83%
tokens_at_position 3.6±0.04ms 3.6±0.07ms 0.00%
tokens_for_file 438.2±4.16µs 400.3±2.73µs -8.65%
traverse 37.8±0.52ms 38.2±0.65ms +1.06%

Copy link

Benchmark for 53dd4f7

Click to view benchmark
Test Base PR %
code_action 5.0±0.07ms 5.1±0.08ms +2.00%
code_lens 296.5±12.39ns 288.3±8.89ns -2.77%
compile 2.7±0.04s 2.8±0.04s +3.70%
completion 4.5±0.06ms 4.6±0.04ms +2.22%
did_change_with_caching 2.5±0.02s 2.6±0.05s +4.00%
document_symbol 868.0±29.00µs 879.8±25.94µs +1.36%
format 70.1±0.89ms 69.2±0.53ms -1.28%
goto_definition 337.5±5.75µs 351.9±4.40µs +4.27%
highlight 8.6±0.08ms 8.7±0.06ms +1.16%
hover 496.4±12.89µs 508.2±7.23µs +2.38%
idents_at_position 119.0±0.50µs 119.7±0.86µs +0.59%
inlay_hints 632.3±37.71µs 642.9±27.53µs +1.68%
on_enter 480.5±28.53ns 475.7±12.49ns -1.00%
parent_decl_at_position 3.5±0.04ms 3.6±0.02ms +2.86%
prepare_rename 337.1±7.79µs 343.2±5.63µs +1.81%
rename 8.9±0.21ms 9.0±0.16ms +1.12%
semantic_tokens 1224.5±11.11µs 1323.3±18.01µs +8.07%
token_at_position 338.1±3.69µs 338.5±3.08µs +0.12%
tokens_at_position 3.6±0.02ms 3.6±0.07ms 0.00%
tokens_for_file 397.5±3.26µs 404.7±4.33µs +1.81%
traverse 37.3±0.82ms 38.3±1.10ms +2.68%

Copy link

Benchmark for b8585c8

Click to view benchmark
Test Base PR %
code_action 5.2±0.17ms 5.1±0.13ms -1.92%
code_lens 292.4±8.12ns 287.5±7.22ns -1.68%
compile 2.8±0.05s 2.8±0.05s 0.00%
completion 4.7±0.12ms 4.7±0.19ms 0.00%
did_change_with_caching 2.7±0.06s 2.7±0.05s 0.00%
document_symbol 907.3±52.55µs 925.9±17.83µs +2.05%
format 70.7±1.09ms 70.5±2.70ms -0.28%
goto_definition 335.8±6.09µs 345.1±9.30µs +2.77%
highlight 8.8±0.15ms 8.8±0.47ms 0.00%
hover 492.5±8.68µs 501.0±6.44µs +1.73%
idents_at_position 119.5±0.31µs 120.7±0.24µs +1.00%
inlay_hints 628.4±30.11µs 632.3±26.32µs +0.62%
on_enter 470.7±28.61ns 467.6±12.39ns -0.66%
parent_decl_at_position 3.6±0.05ms 3.7±0.08ms +2.78%
prepare_rename 339.5±26.26µs 343.4±7.19µs +1.15%
rename 9.3±0.20ms 9.2±0.39ms -1.08%
semantic_tokens 1216.1±9.45µs 1279.7±16.81µs +5.23%
token_at_position 333.1±1.94µs 335.5±6.58µs +0.72%
tokens_at_position 3.6±0.06ms 3.7±0.08ms +2.78%
tokens_for_file 403.9±9.61µs 403.1±6.28µs -0.20%
traverse 38.9±0.91ms 38.3±0.83ms -1.54%

@xunilrj xunilrj changed the title Slice intrinsics: __slice and __slice_elem Slice/Array intrinsics: __slice and __elem_at Jul 27, 2024
Copy link

Benchmark for 18a75c1

Click to view benchmark
Test Base PR %
code_action 5.0±0.10ms 5.1±0.01ms +2.00%
code_lens 286.4±8.38ns 285.4±5.67ns -0.35%
compile 2.6±0.04s 2.6±0.06s 0.00%
completion 4.5±0.08ms 4.5±0.06ms 0.00%
did_change_with_caching 2.6±0.02s 2.5±0.03s -3.85%
document_symbol 875.8±24.24µs 898.6±37.18µs +2.60%
format 68.3±0.79ms 69.1±0.63ms +1.17%
goto_definition 335.2±6.16µs 344.1±6.50µs +2.66%
highlight 8.6±0.01ms 8.7±0.11ms +1.16%
hover 495.9±6.10µs 501.6±10.36µs +1.15%
idents_at_position 119.3±1.96µs 119.0±0.63µs -0.25%
inlay_hints 624.0±25.10µs 627.0±19.14µs +0.48%
on_enter 472.3±18.23ns 467.6±12.17ns -1.00%
parent_decl_at_position 3.6±0.03ms 3.6±0.03ms 0.00%
prepare_rename 331.5±7.40µs 343.2±6.42µs +3.53%
rename 8.9±0.07ms 9.1±0.17ms +2.25%
semantic_tokens 1268.3±16.28µs 1234.0±64.62µs -2.70%
token_at_position 332.5±2.29µs 340.5±3.88µs +2.41%
tokens_at_position 3.6±0.03ms 3.6±0.05ms 0.00%
tokens_for_file 402.4±2.47µs 408.1±2.25µs +1.42%
traverse 36.9±0.75ms 37.1±0.77ms +0.54%

@xunilrj xunilrj marked this pull request as ready for review July 27, 2024 11:31
@xunilrj xunilrj requested review from a team as code owners July 27, 2024 11:31
@xunilrj xunilrj self-assigned this Jul 27, 2024
@xunilrj xunilrj mentioned this pull request Jul 29, 2024
17 tasks
Copy link
Contributor

@IGI-111 IGI-111 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM but these new intrinsics should be documented.

@xunilrj xunilrj requested a review from a team as a code owner July 31, 2024 13:51
Copy link

Benchmark for f6140fd

Click to view benchmark
Test Base PR %
code_action 5.0±0.04ms 5.1±0.12ms +2.00%
code_lens 341.6±8.20ns 286.2±9.85ns -16.22%
compile 2.7±0.04s 2.7±0.04s 0.00%
completion 4.6±0.10ms 4.5±0.08ms -2.17%
did_change_with_caching 2.6±0.06s 2.6±0.05s 0.00%
document_symbol 856.3±20.63µs 871.7±35.28µs +1.80%
format 70.5±0.94ms 72.0±1.39ms +2.13%
goto_definition 333.0±6.47µs 346.5±9.14µs +4.05%
highlight 8.7±0.19ms 8.8±0.18ms +1.15%
hover 490.2±3.83µs 500.6±5.46µs +2.12%
idents_at_position 121.1±0.48µs 118.4±0.65µs -2.23%
inlay_hints 621.4±23.29µs 637.0±28.85µs +2.51%
on_enter 480.3±6.21ns 470.4±14.48ns -2.06%
parent_decl_at_position 3.6±0.05ms 3.6±0.05ms 0.00%
prepare_rename 331.2±7.85µs 344.4±8.54µs +3.99%
rename 8.9±0.05ms 9.0±0.20ms +1.12%
semantic_tokens 1267.7±26.79µs 1271.0±16.68µs +0.26%
token_at_position 335.8±2.63µs 343.1±6.78µs +2.17%
tokens_at_position 3.6±0.03ms 3.6±0.05ms 0.00%
tokens_for_file 404.1±1.96µs 404.9±4.46µs +0.20%
traverse 39.6±0.85ms 39.5±0.78ms -0.25%

Copy link

Benchmark for 2cfce5f

Click to view benchmark
Test Base PR %
code_action 5.2±0.05ms 5.1±0.18ms -1.92%
code_lens 296.9±9.17ns 289.4±12.93ns -2.53%
compile 2.7±0.03s 2.7±0.04s 0.00%
completion 4.7±0.04ms 4.5±0.10ms -4.26%
did_change_with_caching 2.6±0.05s 2.6±0.03s 0.00%
document_symbol 897.3±16.57µs 856.3±31.63µs -4.57%
format 72.2±1.02ms 70.5±0.65ms -2.35%
goto_definition 330.6±3.69µs 336.3±9.28µs +1.72%
highlight 9.0±0.08ms 8.7±0.07ms -3.33%
hover 492.0±8.34µs 490.0±5.10µs -0.41%
idents_at_position 118.5±0.24µs 121.8±0.75µs +2.78%
inlay_hints 635.1±20.73µs 631.4±20.18µs -0.58%
on_enter 2.1±0.05µs 2.0±0.08µs -4.76%
parent_decl_at_position 3.7±0.02ms 3.6±0.03ms -2.70%
prepare_rename 333.6±6.97µs 336.2±8.68µs +0.78%
rename 9.4±0.19ms 9.0±0.15ms -4.26%
semantic_tokens 1277.2±8.89µs 1252.4±32.73µs -1.94%
token_at_position 334.3±1.80µs 334.8±2.44µs +0.15%
tokens_at_position 3.7±0.04ms 3.5±0.03ms -5.41%
tokens_for_file 429.3±2.63µs 450.7±10.84µs +4.98%
traverse 38.5±0.86ms 39.9±1.07ms +3.64%

@xunilrj xunilrj requested a review from IGI-111 August 1, 2024 09:34
IGI-111
IGI-111 previously approved these changes Aug 2, 2024
@IGI-111 IGI-111 requested a review from a team August 2, 2024 02:08
Copy link

github-actions bot commented Aug 4, 2024

Benchmark for 74559cc

Click to view benchmark
Test Base PR %
code_action 5.2±0.10ms 5.0±0.10ms -3.85%
code_lens 293.1±7.90ns 294.9±12.77ns +0.61%
compile 2.6±0.05s 2.7±0.04s +3.85%
completion 4.6±0.06ms 4.5±0.03ms -2.17%
did_change_with_caching 2.6±0.07s 2.6±0.03s 0.00%
document_symbol 962.5±26.97µs 916.7±55.03µs -4.76%
format 68.2±0.81ms 68.8±1.12ms +0.88%
goto_definition 334.4±5.70µs 346.1±5.78µs +3.50%
highlight 9.0±0.35ms 8.7±0.15ms -3.33%
hover 487.9±7.69µs 511.5±7.16µs +4.84%
idents_at_position 119.5±0.29µs 117.7±0.68µs -1.51%
inlay_hints 635.7±38.88µs 627.6±24.97µs -1.27%
on_enter 2.1±0.04µs 2.1±0.06µs 0.00%
parent_decl_at_position 3.6±0.04ms 3.6±0.05ms 0.00%
prepare_rename 332.9±6.84µs 340.7±6.71µs +2.34%
rename 9.2±0.10ms 9.0±0.01ms -2.17%
semantic_tokens 1156.1±37.83µs 1239.2±10.42µs +7.19%
token_at_position 326.6±2.10µs 336.9±4.42µs +3.15%
tokens_at_position 3.6±0.03ms 3.5±0.03ms -2.78%
tokens_for_file 400.4±3.31µs 401.8±3.54µs +0.35%
traverse 38.6±0.81ms 37.8±1.03ms -2.07%

@IGI-111 IGI-111 requested a review from a team August 5, 2024 05:04
Copy link
Contributor

@tritao tritao left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, just some minor suggestions.

sway-core/src/lib.rs Show resolved Hide resolved
sway-ir/src/irtype.rs Show resolved Hide resolved
test/src/e2e_vm_tests/mod.rs Show resolved Hide resolved
Copy link

github-actions bot commented Aug 5, 2024

Benchmark for 00c516f

Click to view benchmark
Test Base PR %
code_action 5.2±0.13ms 5.2±0.03ms 0.00%
code_lens 303.9±8.42ns 343.8±7.88ns +13.13%
compile 2.6±0.05s 2.7±0.05s +3.85%
completion 4.7±0.04ms 4.6±0.09ms -2.13%
did_change_with_caching 2.6±0.02s 2.6±0.02s 0.00%
document_symbol 881.8±13.13µs 913.3±30.59µs +3.57%
format 72.2±0.84ms 73.4±1.70ms +1.66%
goto_definition 337.0±10.52µs 343.4±7.74µs +1.90%
highlight 9.0±0.13ms 9.0±0.19ms 0.00%
hover 501.2±7.59µs 505.1±21.38µs +0.78%
idents_at_position 119.1±0.39µs 120.0±0.64µs +0.76%
inlay_hints 630.1±24.19µs 637.8±25.07µs +1.22%
on_enter 2.1±0.03µs 2.1±0.05µs 0.00%
parent_decl_at_position 3.7±0.12ms 3.7±0.02ms 0.00%
prepare_rename 330.2±6.69µs 337.9±9.12µs +2.33%
rename 9.3±0.08ms 9.2±0.07ms -1.08%
semantic_tokens 1209.3±16.72µs 1246.2±25.01µs +3.05%
token_at_position 335.9±2.71µs 335.8±4.77µs -0.03%
tokens_at_position 3.7±0.04ms 3.6±0.03ms -2.70%
tokens_for_file 400.8±3.94µs 402.0±4.10µs +0.30%
traverse 38.1±1.06ms 39.0±0.64ms +2.36%

@IGI-111 IGI-111 enabled auto-merge (squash) August 7, 2024 08:45
@IGI-111 IGI-111 merged commit afd6a67 into master Aug 7, 2024
43 checks passed
@IGI-111 IGI-111 deleted the xunilrj/slice-intrinsics branch August 7, 2024 09:00
Copy link

github-actions bot commented Aug 7, 2024

Benchmark for e26acde

Click to view benchmark
Test Base PR %
code_action 5.1±0.10ms 5.1±0.11ms 0.00%
code_lens 328.8±10.57ns 298.6±4.99ns -9.18%
compile 2.7±0.04s 2.6±0.05s -3.70%
completion 4.6±0.10ms 4.6±0.07ms 0.00%
did_change_with_caching 2.6±0.04s 2.6±0.03s 0.00%
document_symbol 842.8±18.32µs 924.1±23.42µs +9.65%
format 72.8±0.98ms 73.2±1.70ms +0.55%
goto_definition 327.9±6.46µs 348.2±6.51µs +6.19%
highlight 8.7±0.15ms 8.7±0.16ms 0.00%
hover 482.2±6.53µs 501.6±6.81µs +4.02%
idents_at_position 119.7±0.54µs 117.8±1.02µs -1.59%
inlay_hints 619.7±25.30µs 632.6±26.63µs +2.08%
on_enter 2.0±0.03µs 1998.8±69.38ns -0.06%
parent_decl_at_position 3.6±0.04ms 3.6±0.04ms 0.00%
prepare_rename 354.9±6.95µs 349.0±10.76µs -1.66%
rename 9.0±0.15ms 9.0±0.21ms 0.00%
semantic_tokens 1214.9±15.19µs 1250.0±52.83µs +2.89%
token_at_position 329.8±2.72µs 347.6±2.34µs +5.40%
tokens_at_position 3.6±0.03ms 3.6±0.02ms 0.00%
tokens_for_file 396.7±3.67µs 404.1±3.69µs +1.87%
traverse 38.1±0.51ms 37.7±0.49ms -1.05%

esdrubal pushed a commit that referenced this pull request Aug 13, 2024
## Description

This PR is part of #5110 and
introduces two new intrinsic: `__slice` and `__elem_at`.

`__slice` allows the creation of slices by slicing arrays or other
slices. Whilst `__elem_at` returns a reference to an item inside the
slice or the array.

## Out of bounds checks

These intrinsic will not generate any runtime checks, these must be done
manually, when and where appropriate; but they do a complete static
analysis of all indices, to avoid runtime buffer overflows, when
possible.

That means that at runtime, it is possible to do a buffer overflow when
reading/writing, which is an "undefined behaviour" as to what will
happen.

## Empty Array

This PR also solves a problem with empty arrays. Before empty arrays
such as `let a = []` were being type-checked as `[Never; 0]`, which
means that any code after them was being marked as dead.

Now we correctly type check them as `[Unknown; 0]` and return a more
friendly error.

```
          4 |
          5 |     // Empty array
          6 |     let a = [];
            |             ^^ Type must be known at this point
          7 | }
            |
          ____
```

## Check of constants inside fns

This PR also solves a problem with not checking `const` expressions
inside `fns`. We, for example, do not allow slices in constants, but we
were only checking globals. Now we check constants also inside
functions, methods etc...

## Small improvements for our e2e

We can now `dbg` inside our e2e harness and get results like the ones
below. One needs to include the lib `test/src/e2e_vm_tests/utils` and
cal `something.dbg()` or `something.dbgln()`. There is no magic, and
structs/enums will need to manually implement the `Dbg` trait.

This is only to facilitate the debugging of our e2e tests.


![image](https://github.com/user-attachments/assets/2f25c50e-b7b3-4199-8bf4-699473919e6c)


## Checklist

- [ ] I have linked to any relevant issues.
- [ ] I have commented my code, particularly in hard-to-understand
areas.
- [ ] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [ ] I have added tests that prove my fix is effective or that my
feature works.
- [ ] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [ ] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [ ] I have requested a review from the relevant team or maintainers.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants